//提示信息对象
let msg;
//列对象
let cols;

layui.config({
    base: '/lay/dist/'
});
layui.use(['form', 'table', 'laydate', 'notice','element'], function () {
    var $ = layui.jquery,
        form = layui.form,
        table = layui.table,
        laydate = layui.laydate,
        notice = layui.notice,
            element = layui.element;

    msg = layui.notice;
    let curr_url;
    let currentState;
    /**
     * 表格主题渲染
     */
    element.render();
       $(function () {
            curr_url=""
            currentState =parseInt($("#state").text());
           console.log(currentState)
           switch (currentState) {
               case 0: //班主任；
                   curr_url = "getLifeTch";
                   break
               case 1: //教员
                   curr_url = "getTch";
                   break;
               case 3:
                   curr_url = "getDeTch";
                   break
               default:
                   msg.error("抱歉当前账号没有权利访问这个页面")
                   return;
           }
           table.render({
               elem: '#exam',
               url: '/society-for/'+curr_url,
               method: 'post',
               text:{none:"当前没有要处理的学社申请"},
               toolbar: '#toolbarDemo',
               defaultToolbar: ['filter', 'exports', 'print', {
                   title: '提示',
                   layEvent: 'LAYTABLE_TIPS',
                   icon: 'layui-icon-tips'
               }],
               cols: [
                   [
                       {type: 'checkbox', fixed: 'left'},
                       {field: 'studentid',hide: true},
                       {
                           field: 'xid', width: 80, title: '序号', sort: true, templet: function (d) {
                               return d.LAY_INDEX;
                           }
                       },
                       {field: 'id', width: 100, title: 'ID', sort: true, hide: true},
                       {field: 'studentName',width: 100,title: '姓名'},
                       {field: 'classname',width: 100, title: '班级',templet:function (result) {
                               return result.grade.classname;
                           }},
                       {field: 'semesterName',width: 100,title: '阶段',templet:function (result) {
                               return result.grade.semesterName
                           } },
                       {field: 'dename',width: 100,title: '申请部门',templet:function (result) {
                               return result.departments.dename;
                           }},
                       {field: 'usersuggest',width: 150,title: '对学社的看法'},
                       {field: 'evaluation',width: 150,title: '自我评价'},
                       {field: 'updatetime',width: 120,title: '处理时间',templet:function (result) {
                               return layui.laytpl.toDateString(result.updatetime);
                           }},
                       {
                           field: 'applyState', width: 150, title: '进度', align: 'center', templet: function (result) {
                               return
                                   '  <div class="layui-progress-bar layui-bg-orange" lay-percent="'+parseInt((result.applystate)*20)+'%"></div>'
                           }},
                       {title: '操作', minWidth:200 , toolbar: '#currentTableBar', align: "center",fixed: 'right'}

                   ]
               ],
               limits: [10, 15, 20, 25, 50, 100],
               limit: 15,
               page: true,
               skin: 'line'
               ,done:function (res,currentCount) {
                   //***重点***：table渲染完成后渲染element进度条
                   element.render()
               }
           });

       })



    function getDisposeUrl(state) {
           switch (state) {
               case 0:
                   return "/society-for/disposeByLifeTch"
                   break;
               case 1:
                   return "/society-for/disposeByTch"
                   break;
               case 3:
                   return "/society-for/disposeByDeTch"
                   break;
               default:
                   return ""
                   break
           }
    }
    let curtId;
    //监听提交
    form.on('submit(saveBtn)', function(data){
        let d=data.field;
        d.id=curtId;
        d.dispose=d.dispose||0;
        $.post(getDisposeUrl(currentState),d,res=>{
            if (res.success) {
                msg.success("处理成功")
                layer.closeAll()
                table.reload("exam",{
                    url:'/society-for'+curr_url
                })
            } else {
                msg.error("处理失败:"+res.data)
            }
        })
        return false;
    });







    /**
     * 弹框捕捉页面元素；
     * @param target  显示的目标
     * @param width  显示的宽度
     * @param height  显示的高度
     * @param ofTop   偏移量Y  默认是鼠标的位置
     * @param ofLeft  偏移量X
     * @param cancelFun
     * @returns {*}
     */
    function showPopout(target,width,height,ofTop,ofLeft,cancelFun) {
        let viewX=window.event.clientX+ofLeft;
        let viewY=window.event.clientY+ofTop;
      return layer.open({
            type: 1,
            shade: false,
            area: [width, height],
            offset: [viewY+'px', viewX+'px'],
            shade:0.5,
            title: false, //不显示标题
            content: target, //捕获的元素，注意：最好该指定的元素要存放在body最外层，否则可能被其它的相对元素所影响
            cancel: cancelFun
        });
    }
    /**
     * toolbar监听事件
     */
    table.on('toolbar(currentTableFilter)', function (obj) {
        if (obj.event === 'selectAll') { //
        }else if (obj.event==='notice'){  //批量处理
            let data = table.checkStatus(obj.config.id).data;
        }



    });

    /**
     * 工具行监听(删除/编辑)
     * */
    table.on('tool(currentTableFilter)', function (obj) {
        let data = obj.data;
        cols = obj;
        if (obj.event === 'info') {
            let index = layer.open({
                title: false,
                type: 2,
                shade: 0.2,
                maxmin: true,
                shadeClose: true,
                area: ['70%', '95%'],
                content: '/society-for/information?id=' + data.id,
            });
            $(window).on("resize", function () {
                layer.full(index);
            });
            return false;
        } else if (obj.event === 'dispose') {
            curtId=data.id;
            //捕获页
            layer.open({
                type: 1,
                shade: 0.5,
                title: "处理申请", //不显示标题
                area:["50%","40%"],
                content: $('#dispose'), //捕获的元素，注意：最好该指定的元素要存放在body最外层，否则可能被其它的相对元素所影响
                cancel: function(){
                    layer.msg('处理已取消', {time: 1000, icon:6});
                }
            });
        }
    });
    /**
     * 时间戳的处理
     * @param d
     * @param format
     * @returns {string}
     */
    layui.laytpl.toDateString = function (d, format) {
        var date = new Date(d || new Date())
            , ymd = [
            this.digit(date.getFullYear(), 4)
            , this.digit(date.getMonth() + 1)
            , this.digit(date.getDate())
        ]
            , hms = [
            this.digit(date.getHours())
            , this.digit(date.getMinutes())
            , this.digit(date.getSeconds())
        ];

        format = format || 'yyyy-MM-dd';

        return format.replace(/yyyy/g, ymd[0])
            .replace(/MM/g, ymd[1])
            .replace(/dd/g, ymd[2])
            .replace(/hh/g, hms[0])
            .replace(/mm/g, hms[1])
            .replace(/ss/g, hms[2]);
    };
    /**
     * 数字前置补零
     * @param num
     * @param length
     * @param end
     * @returns {*}
     */
    layui.laytpl.digit = function (num, length, end) {
        var str = '';
        num = String(num);
        length = length || 2;
        for (var i = num.length; i < length; i++) {
            str += '0';
        }
        return num < Math.pow(10, length) ? str + (num | 0) : num;
    };
    /**
     * 时间选择器自定义颜色
     */
    laydate.render({
        elem: '#begin'

        , theme: '#393D49'
    });
    laydate.render({
        elem: '#end'

        , theme: '#393D49'
    });


    /**
     * 设置提示信息属性
     * @type {{hideEasing: string, positionClass: string, hideDuration: string, debug: boolean, closeButton: boolean, extendedTimeOut: string, showEasing: string, onclick: null, showDuration: string, iconClass: string, timeOut: string}}
     */
    notice.options = {
        closeButton: true,//显示关闭按钮
        debug: false,//启用debug
        positionClass: "toast-bottom-right",//弹出的位置,
        showDuration: "300",//显示的时间
        hideDuration: "1000",//消失的时间
        timeOut: "2000",//停留的时间
        extendedTimeOut: "1000",//控制时间
        showEasing: "swing",//显示时的动画缓冲方式
        hideEasing: "linear",//消失时的动画缓冲方式
        iconClass: 'toast-info', // 自定义图标，有内置，如不需要则传空 支持layui内置图标/自定义iconfont类名
        onclick: null, // 点击关闭回调
    };

    // notice.warning("");信息状态
    // notice.info("");
    // notice.error("");
    // notice.success("");


});


